<?php
session_start();
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
if(!isset($_SESSION['name'])||empty ( $_SESSION['name'])){
     header('Location: login.php');
}
include '../../functions.php';
if(isset($_GET['search']))
{
    $filename=clear($_GET['filename']);
    $owner=clear($_GET['owner']);
    $date=clear($_GET['date']);
}
else
{
     $filename="";
      $owner="";
      $date="";
}
        $search="";
        $searchTitle="";
        if(!empty($filename))
        {
            $searchTitle.="File : $filename";
            $search.="and `filename`='$filename' ";
        }
        if(!empty($owner))
        {
            $searchTitle.=" , Owner : $owner";
            $search.="and `owner`='$owner' ";
        }
        if(!empty($date))
        {
            $searchTitle.=" , upload date : $date";
            $search.="and `uploadDate`='$date' ";
        }
?>
 <link rel="stylesheet" href="css/calendar.css">

 <link rel="stylesheet" href="css/style.css">

<script language="JavaScript" src="js/calendar_db.js" ></script>
<script src="jquery-1.5.1.js"></script>
<script src="js/view.js"></script>

<form method="get" name='newPackage'>
    <table>
        <tr><td>FileName</td><td>Owner</td><td>Date</td></tr>
        <tr><td><input type="text" name="filename" value="<?=$filename?>" /></td>
            <td><input type="text" name="owner" value="<?=$owner?>" /></td>
            <td><input type="text" name="date" value="<?=$date?>" />
                
                	<script>
                	new tcal ({
                		// form name
                		'formname': 'newPackage',
                		// input name
                		'controlname': 'date'
                	});
                        </script>
            </td>
        </tr>
        <tr><td colspan="3"><input type="submit" name="search" value="search" /></td></tr>

    </table>
</form>
<div>
<?php
if(!empty ($searchTitle))
{
    echo "you search for $searchTitle";
}
?>
</div>
<a href="upload.php">ADD</a>&nbsp;&nbsp;&nbsp;<a href="view.php">show all</a>&nbsp;&nbsp;&nbsp;<a href="myfav.php">myfavoreites</a>
<table cellpadding="10" border="1" >

    <tr><td>FileName</td><td>Owner</td><td>UploadAt</td><td>addToFavoriet</td><td>Dolwnload</td></tr>
    <?php
        include 'connect.php';

        $itemSet=  mysql_query("select files.id,filename,name,uploadDate,code
                                from users,files
                                where users.id=files.user_id $search");
        $favoritesSet=mysql_query("select * from favorites");

        $favArray=array();
        $i=0;
        $inmyfav="oumyfav";
        while($fav=  mysql_fetch_array($favoritesSet))
        {
            $favs[$i][0]=$fav['user_id'];
            $favs[$i][1]=$fav['file_id'];
            $i++;
        }
        
        while($item=mysql_fetch_array($itemSet))
        {
            $inmyfav="outmyfav";
            $class='nofav';
            foreach($favs as $k=>$v)
            {
                for($i=0;$i<2;$i++)
                {
                    if($_SESSION['id']==$v[0]&&$item['id']==$v[1])
                    {
                        $class='myfav';
                        $inmyfav="inmyfav";
                    }
                }
            }
            $downloadLink="<a href=\"mmm/$item[code]\">download</a>";
            echo "<tr><td>$item[filename]</td><td>$item[name]</td><td>$item[uploadDate]</td><td><span id=\"$item[id]\" class=\"$class\">$inmyfav</span></td><td>$downloadLink</td>";
        }
    ?>
</table>